
#include <windows.h>
//#include <stdexcept>
#include <wchar.h>
#include "Service.h"
#include "Log.h"
#include "control.h"
#include "config.h"

namespace wrservice
{

/////////////////////////////////////////////////////////////////////
//
// Goal : 
//   Installs a service in the SCM database
//
void service_install (Service& svc)
{
    SC_HANDLE	schSCManager, schService;
    wchar_t		svcPath[MAX_PATH];
	
    if( !::GetModuleFileName( NULL, svcPath, MAX_PATH ) ) {
        svc.get_log() << "Cannot install service";
        //throw std::runtime_error ("Cannot install service");
		wprintf (L"Cannot install service");
		return;
    }
	
    schSCManager = OpenSCManager(
        NULL, NULL, SC_MANAGER_ALL_ACCESS);
 
    if ( NULL == schSCManager ) 
    {
        svc.get_log() << "OpenSCManager failed";
        //throw std::runtime_error ("OpenSCManager failed");
        return;
    }

    schService = CreateService(
        schSCManager,               // SCM database
        svc.get_name(),             // name of service
        svc.get_name(),             // service name to display
        SERVICE_ALL_ACCESS,         // desired access
        SERVICE_WIN32_OWN_PROCESS,  // service type
        SERVICE_DEMAND_START,       // start type
        SERVICE_ERROR_NORMAL,       // error control type
        svcPath,                     // path to service's binary
        NULL,                       // no load ordering group
        NULL,                       // no tag identifier
        NULL,                       // no dependencies
        NULL,                       // LocalSystem account
        NULL                        // no password
    );
 
    if ( NULL == schService )
    {
        svc.get_log() << "CreateService failed";
        //throw std::runtime_error ("CreateService failed");
        CloseServiceHandle(schSCManager);
        return;
    }
    else {
        svc.get_log().write_event ("Service installed successfully");
        wprintf(L"Service installed successfully\n");
    }
    CloseServiceHandle(schService); 
    CloseServiceHandle(schSCManager);
}

/////////////////////////////////////////////////////////////////////
//
// Goal : 
//   Deletes a service from the SCM database
//
void service_uninstall (Service& svc)
{
    SC_HANDLE schSCManager;
    SC_HANDLE schService;
    
    schSCManager = OpenSCManager(
        NULL, NULL, SC_MANAGER_ALL_ACCESS);
 
    if (NULL == schSCManager)
    {
        svc.get_log() << "OpenSCManager failed";
        //throw std::runtime_error ("OpenSCManager failed");
        return;
    }

    schService = OpenService(
        schSCManager, svc.get_name(), DELETE);
 
    if (schService == NULL)
    {
        svc.get_log() << "OpenService failed";
        //throw std::runtime_error ("OpenService failed");
        CloseServiceHandle(schSCManager);
        return;
    }

    if (! DeleteService(schService) )
    {
        svc.get_log() << "DeleteService failed";
        //throw std::runtime_error ("DeleteService failed");
    }
    else { 
        svc.get_log() << "Service deleted successfully\n";
        wprintf( L"Service deleted successfully\n" );
        //DeleteFile (szLogFile);
    }
 
    CloseServiceHandle(schService); 
    CloseServiceHandle(schSCManager);
}

void service_usage (Service& svc)
{
    wprintf ( 
        L"Description:\n"
        L"    Winsock Redirection Service for RDS (mstsc.exe)\n\n"
        L"Usage:\n" );
    wprintf ( 
        L"    %s [command]\n\n", svc.get_name() );
    wprintf ( 
		L"    [command]\n"
		L"        help\n" );
    config_usage();
    control_usage();
}


void config_usage ()
{
    wprintf ( 
		L"        install\n"
		L"        uninstall\n"
	);
}

} /* namespace wrservice */
